package com.lian.anlimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lian.anlimall.order.constant.OrderConstant;
import com.lian.anlimall.order.dao.OrderDao;
import com.lian.anlimall.order.dao.OrderItemDao;
import com.lian.anlimall.order.entity.OrderEntity;
import com.lian.anlimall.order.entity.OrderItemEntity;
import com.lian.anlimall.order.service.OrderItemService;
import com.lian.anlimall.order.service.OrderService;
import com.lian.anlimall.order.vo.tuihuoVo;
import com.lian.common.utils.PageUtils;
import com.lian.common.utils.Query;
import com.lian.anlimall.order.dao.OrderReturnApplyDao;
import com.lian.anlimall.order.entity.OrderReturnApplyEntity;
import com.lian.anlimall.order.service.OrderReturnApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service("orderReturnApplyService")
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyDao, OrderReturnApplyEntity> implements OrderReturnApplyService {
    @Autowired
    public OrderItemService orderItemService;
    @Autowired
    public OrderService orderService;
    @Autowired
    public OrderDao orderDao;
    @Autowired
    public OrderItemDao orderItemDao;

    @Autowired
    public OrderReturnApplyService orderReturnApplyService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderReturnApplyEntity> page = this.page(
                new Query<OrderReturnApplyEntity>().getPage(params),
                new QueryWrapper<OrderReturnApplyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void save(tuihuoVo tuihuoVo) {

        List<String> arr = Arrays.asList(tuihuoVo.getTest().split("="));
        String picUrl = "";
        for  (String item: arr){
            if(item.equals("picture")){
                continue;
            }

            if(picUrl.length()==0||picUrl.equals(null)){
                picUrl = item;
            }
            else {
                picUrl = picUrl +','+ item;
            }

        }

       OrderItemEntity orderItemEntity= orderItemDao.ListBySnAndSkuId( tuihuoVo.getOrderSn(), tuihuoVo.getSkuId());

       OrderReturnApplyEntity orderReturnApplyEntity = new OrderReturnApplyEntity();
       orderReturnApplyEntity.setCreateTime(new Date());
       orderReturnApplyEntity.setOrderSn(tuihuoVo.getOrderSn());
       orderReturnApplyEntity.setSkuId(orderItemEntity.getSkuId());
       orderReturnApplyEntity.setSkuAttrsVals(orderItemEntity.getSkuAttrsVals());
       orderReturnApplyEntity.setSkuBrand(orderItemEntity.getSpuBrand());
       orderReturnApplyEntity.setSkuCount(orderItemEntity.getSkuQuantity());
       orderReturnApplyEntity.setSkuImg(orderItemEntity.getSkuPic());
       orderReturnApplyEntity.setSkuName(orderItemEntity.getSkuName());
        orderReturnApplyEntity.setSkuPrice(orderItemEntity.getSkuPrice());
        orderReturnApplyEntity.setSkuRealPrice(orderItemEntity.getRealAmount());
        orderReturnApplyEntity.setDescPics(picUrl);
        orderReturnApplyEntity.setReason(tuihuoVo.getReasonRadio());
        orderReturnApplyEntity.setDescription(tuihuoVo.getReason());
        orderReturnApplyEntity.setReturnAmount(orderItemEntity.getRealAmount().multiply(new BigDecimal(orderItemEntity.getSkuQuantity())));
        OrderEntity orderEntity = orderService.getOrderByOrderSn(tuihuoVo.getOrderSn());

        orderReturnApplyEntity.setReturnName(orderEntity.getReceiverName());
        orderReturnApplyEntity.setReturnPhone(orderEntity.getReceiverPhone());
        orderReturnApplyEntity.setStatus(OrderConstant.tuihuoStatus_daishenhe);

        orderReturnApplyService.save(orderReturnApplyEntity);
        System.out.println("保存成功"+orderReturnApplyEntity);
        //TODO 修改订单状态
    }

    @Override
    public void shenhe(Long id, Integer userId, String userName) {
        OrderReturnApplyEntity orderReturnApplyEntity = orderReturnApplyService.getById(id);
        orderReturnApplyEntity.setStatus(OrderConstant.tuihuoStatus_yiwancheng);
        orderReturnApplyEntity.setHandleTime(new Date());
        orderReturnApplyEntity.setHandleMan(userName);
        orderReturnApplyService.updateById(orderReturnApplyEntity);
    }

}